package com.sne.sys.controller;

import com.sne.common.entity.Result;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sne.sys.dto.SysParamDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sne.common.validated.Insert;
import com.sne.common.validated.Update;
import com.sne.sys.entity.SysParam;
import com.sne.sys.service.ISysParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Size;
import javax.validation.groups.Default;
import java.util.List;

/**
 * 系统参数前端控制器
 * @author fanghh
 * @since 2020-01-10
 */
@Validated
@RestController
@RequestMapping("/sys/sys-param")
@Api(tags = "系统参数")
public class SysParamController{

    @Autowired
    private ISysParamService service;

    @PreAuthorize("hasAuthority('sys:param:page')")
    @ApiOperation(value = "获取系统参数分页列表")
    @PostMapping("/page")
    public Result page(Page page, @RequestBody SysParamDTO entity){
        return Result.success(service.findPage(page,entity));
    }

    @PreAuthorize("hasAuthority('sys:param:save')")
    @ApiOperation(value = "保存系统参数")
    @PostMapping("/save")
    public Result save(@Validated({Default.class,Insert.class}) @RequestBody SysParam entity){
        return Result.success(service.save(entity));
    }

    @PreAuthorize("hasAuthority('sys:param:update')")
    @ApiOperation(value = "更新系统参数")
    @PutMapping("/update")
    public Result update(@Validated({Update.class}) @RequestBody SysParam entity){
        return Result.success(service.updateById(entity));
    }

    @ApiOperation(value = "根据id获取系统参数")
    @GetMapping("/find/{id}")
    public Result find(@PathVariable("id")Long id){
        return Result.success(service.getById(id));
    }

    @PreAuthorize("hasAuthority('sys:param:delete')")
    @ApiOperation(value = "删除系统参数")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        return Result.success(service.removeById(id));
    }

    @PreAuthorize("hasAuthority('sys:param:deleteIds')")
    @ApiOperation(value = "删除多个系统参数")
    @DeleteMapping("/deleteIds")
    public Result deleteIds(@RequestBody @Size(min = 1) List<Long> ids){
        return Result.success(service.removeByIds(ids));
    }
}



